package com.qf.leadnewswemedia.interceptors;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.qf.leadnewsutils.commons.ThreadLocalUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UidHeaderInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String uid = request.getHeader("uid");

        if (StringUtils.isNotEmpty(uid))
            ThreadLocalUtil.saveUid(Long.parseLong(uid));

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //防止内存泄漏
        ThreadLocalUtil.removeUid();
    }
}
